package at.fhhgb.mc.android.mfb_hgb.activity.dialog;

import java.util.Calendar;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.EditText;
import android.widget.TimePicker;
import at.fhhgb.mc.android.mfb_hgb.R;

public class TimePickerDialog extends DialogFragment implements OnTimeSetListener {
	
	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        int hour;
        int minute;
       
        
        if(getArguments() != null && getArguments().containsKey("defaultTime"))
        {
        	Calendar defaultTime = (Calendar)getArguments().getSerializable("defaultTime");
        	
        	hour = defaultTime.get(Calendar.HOUR_OF_DAY);
        	minute = defaultTime.get(Calendar.MINUTE);
        }
        else
        {
        	 Calendar c = Calendar.getInstance();
        	
        	 hour = c.get(Calendar.HOUR_OF_DAY);
             minute = c.get(Calendar.MINUTE);
        }

        // Create a new instance of TimePickerDialog and return it
        return new android.app.TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
    }

	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		EditText time = (EditText)getActivity().findViewById(getArguments().getInt("editTextKey"));
		time.setText(String.format("%02d",hourOfDay) + ":" + String.format("%02d",minute));
	}
}
